package src.com.qf.service.impl;

import src.com.qf.dao.GoodCarDao;
import src.com.qf.dao.GoodDao;
import src.com.qf.dao.impl.GoodCarDaoImpl;
import src.com.qf.dao.impl.GoodDaoImpl;
import src.com.qf.entity.Good;
import src.com.qf.entity.GoodCar;
import src.com.qf.service.GoodCarService;
import src.com.qf.utils.JdbcUtil;

import java.math.BigDecimal;
import java.util.List;

public class GoodCarServiceImpl implements GoodCarService {
    private GoodCarDao dao = new GoodCarDaoImpl();
    private GoodDao goodDao = new GoodDaoImpl();
    @Override
    public int addCart(GoodCar car) {
        //先判断用户有无添加过商品到购物车
        //如果添加过，则执行update
        //如果未添加过，则执行insert
        int result = 0;
        try {
            JdbcUtil.begin();
            GoodCar oldCar = dao.selectGoodCarByUidAndGoodId(car);

            if(oldCar == null){
                result = dao.insertGoodCar(car);
            }else {
                oldCar.setGoodNum(oldCar.getGoodNum()+1);
                BigDecimal newGoodeSum = oldCar.getGoodSum().add(car.getGoodSum());
                oldCar.setGoodSum(newGoodeSum);
                result = dao.updateGoodNumAndGoodSumById(oldCar);
            }
            JdbcUtil.commit();
        } catch (Exception e) {
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public List<GoodCar> getCarts(Integer uid) {

        List<GoodCar> list = null;
        try{
            JdbcUtil.begin();

            list = dao.selectGoodCarsByUid(uid);

            for (GoodCar c : list) {
                Good good = goodDao.selectGoodById(c.getGoodId());
                c.setGood(good);
            }


            JdbcUtil.commit();
        } catch (Exception e){
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int updateCartNum(int goodId, BigDecimal goodPrice, int num, Integer uid) {
        int result = 0;
        try {
            JdbcUtil.begin();
            GoodCar goodCar = new GoodCar();
            goodCar.setGoodId(goodId);
            goodCar.setUid(uid);
            GoodCar newGoodCar = dao.selectGoodCarByUidAndGoodId(goodCar);
            if(num == 1){
                //增加
                newGoodCar.setGoodNum(newGoodCar.getGoodNum()+num);
                newGoodCar.setGoodSum(newGoodCar.getGoodSum().add(goodPrice));
                result = dao.updateGoodNumAndGoodSumById(newGoodCar);
            }else if (num == -1){
                //减少
                newGoodCar.setGoodNum(newGoodCar.getGoodNum()+num);
                newGoodCar.setGoodSum(newGoodCar.getGoodSum().subtract(goodPrice));
                result = dao.updateGoodNumAndGoodSumById(newGoodCar);
            }else {
                //删除
                result = dao.deleteGoodCarById(newGoodCar.getId());
            }
            JdbcUtil.commit();
        } catch (Exception e) {
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int clearCart(int id) {
        int result = 0;
        try{
            JdbcUtil.begin();

            result = dao.deleteGoodCarById(id);


            JdbcUtil.commit();
        } catch (Exception e){
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public int clearAllCar(Integer uid) {
        int result = 0;
        try{
            JdbcUtil.begin();

            result = dao.deleteGoodCarByUid(uid);


            JdbcUtil.commit();
        } catch (Exception e){
            JdbcUtil.rollback();
            e.printStackTrace();
        }
        return result;
    }
}
